package com.wmlive.hhvideo.heihei.discovery.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wmlive.hhvideo.heihei.beans.discovery.TopicTypeListBean;
import com.wmlive.hhvideo.heihei.beans.main.ShortVideoItem;
import com.wmlive.hhvideo.heihei.discovery.DiscoveryUtil;
import com.wmlive.hhvideo.heihei.discovery.viewholder.DiscoveryViewHolder;
import com.wmlive.hhvideo.utils.CollectionUtil;
import com.wmlive.hhvideo.utils.DeviceUtils;
import com.wmlive.hhvideo.utils.imageloader.GlideLoader;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshAdapter;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshRecyclerView;

import java.util.List;

import cn.wmlive.hhvideo.R;

/**
 * Created by lsq on 5/27/2017.
 * 发现的Adapter
 */

public class DiscoveryAdapter extends RefreshAdapter<DiscoveryViewHolder, TopicTypeListBean.TopicListBean> {

    private OnPictureClickListener onPictureClickListener;

    private LinearLayout.LayoutParams imageLayoutParams;
    private LinearLayout.LayoutParams textLayoutParams;
    private int iconSize;
    private int imagePadding;

    public DiscoveryAdapter(List<TopicTypeListBean.TopicListBean> list, RefreshRecyclerView refreshView) {
        super(list, refreshView);
        imageLayoutParams = new LinearLayout.LayoutParams(DeviceUtils.dip2px(refreshView.getContext(), 105),
                LinearLayout.LayoutParams.MATCH_PARENT);
        iconSize = DeviceUtils.dip2px(refreshView.getContext(), 14);
        imagePadding = 8;
    }

    @Override
    public DiscoveryViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new DiscoveryViewHolder(parent, R.layout.item_discovery);
    }

    @Override
    public void onBindHolder(DiscoveryViewHolder holder, final int position, TopicTypeListBean.TopicListBean data) {
        if (data == null) {
            data = TopicTypeListBean.newTopicListBean();
        }
        final TopicTypeListBean.TopicListBean bean = data;
        holder.tvCount.setText(String.valueOf(bean.getOpus_count()));

        int drawableId = 0;
        if (!TextUtils.isEmpty(bean.getTopic_type())) {
            switch (bean.getTopic_type()) {
                case "Topic":
                    holder.tvTopicType.setTextColor(holder.tvTopicType.getResources().getColor(R.color.hh_color_g));
                    drawableId = R.drawable.icon_find_topic;
                    holder.tvTopicType.setText(R.string.stringTopic);
                    break;
                case "Music":
                    holder.tvTopicType.setTextColor(holder.tvTopicType.getResources().getColor(R.color.hh_color_f));
                    drawableId = R.drawable.icon_find_music;
                    holder.tvTopicType.setText(R.string.stringMusic);
                    break;
            }
            holder.llTypePanel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onPictureClickListener && !TextUtils.isEmpty(bean.getTopic_type())) {
                        switch (bean.getTopic_type()) {
                            case "Topic":
                                onPictureClickListener.onTopicClick(position, true, bean.getId());
                                break;
                            case "Music":
                                onPictureClickListener.onTopicClick(position, false, bean.getDefault_music_id());
                                break;
                        }
                    }
                }
            });
        } else {
            holder.tvTopicType.setText("");
        }
        DiscoveryUtil.setDrawable(holder.tvTopicType, drawableId, 0, iconSize, iconSize);
        holder.tvTopicName.setText(bean.getName());
        holder.llContainer.removeAllViews();
        if (!CollectionUtil.isEmpty(bean.getTopic_opus_list())) {
            ImageView imageView;
            ShortVideoItem shortVideoItem;
            int n = bean.getTopic_opus_list().size();
            for (int i = 0, length = (n > 10 ? 10 : n); i <= n; i++) {
                final int index = i;
                if (i < length) {
                    shortVideoItem = bean.getTopic_opus_list().get(i);
                    if (shortVideoItem != null) {
                        imageView = (ImageView) LayoutInflater.from(holder.llContainer.getContext()).inflate(R.layout.view_discovery_picture, holder.llContainer, false);
//                        imageView.setPadding(i != 0 ? imagePadding : 0, imagePadding, imagePadding, imagePadding);
                        imageLayoutParams.rightMargin = i != 0 ? imagePadding : 0;
                        holder.llContainer.addView(imageView, imageLayoutParams);
                        GlideLoader.loadImage(shortVideoItem.getOpus_small_cover(), imageView, R.drawable.icon_bg_video_default);
                        imageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (null != onPictureClickListener) {
                                    onPictureClickListener.onPictureClick(bean.getId(), bean.getTopic_opus_list(), bean.getTopic_opus_list().get(index).getId(), index);
                                }
                            }
                        });
                    }
                } else if (i == 10) {
                    View view = LayoutInflater.from(holder.llContainer.getContext()).inflate(R.layout.view_discovery_more, holder.llContainer, true);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (null != onPictureClickListener && !TextUtils.isEmpty(bean.getTopic_type())) {
                                switch (bean.getTopic_type()) {
                                    case "Topic":
                                        onPictureClickListener.onTopicClick(position, true, bean.getId());
                                        break;
                                    case "Music":
                                        onPictureClickListener.onTopicClick(position, false, bean.getDefault_music_id());
                                        break;
                                }
                            }
                        }
                    });
                } else {
                    break;
                }
            }
        }
    }

    public void setOnPictureClickListener(OnPictureClickListener onPictureClickListener) {
        this.onPictureClickListener = onPictureClickListener;
    }

    public interface OnPictureClickListener {
        void onPictureClick(long topicId, List<ShortVideoItem> listBean, long videoId, int position);

        void onTopicClick(int position, boolean isTopic, long id);
    }

}
